<?php  defined('SYSPATH') or die('No direct script access.');
/**
 * 权限相关
 * User: kang
 * Date: 16/2/25
 * Time: 18:12
 */
class Controller_Auth extends Controller_Base
{
    private $node;

    //获取所有角色
    public function action_role_show()
    {
        $role = Model::factory('Auth')->roleShow();
        !$role ? Public_Helper::response(201) : Public_Helper::response(0,$role);
    }

    public function action_role_add()
    {
        $param = array(
            'name'  =>  $this->request->post('name')
        );
        //添加角色
        $addId = Model::factory('Auth')->roleAdd($param);
        $addId===false && Public_Helper::response(201);
        //更新角色权限
        $nid = explode(',',$this->request->post('node'));
        $up = Model::factory('Auth')->updateRoleNode($addId, $nid);
        !$up ? Public_Helper::response(201):Public_Helper::response(0);
    }

    public function action_role_delete()
    {
        //检查是否有用户属于本组,有的话禁止删除
        $role = (int) $this->request->post('role');
        $config = Kohana::$config->load('base')->as_array();
        if(isset($config['adminId']) && intval($config['adminId'])===$role){
            Public_Helper::response(102);
        }

        $user = Model::factory('User')->getUserByRole($role);
        $user===false && Public_Helper::response(201);
        !empty($user) && Public_Helper::response(102);

        $del = Model::factory('Auth')->roleDelete($role);
        !$del ? Public_Helper::response(201):Public_Helper::response(0);
    }


    //更新角色权限
    public function action_update_role_node()
    {
        $role = (int) $this->request->post('role');
        $node = $this->request->post('node');
        $config = Kohana::$config->load('base')->as_array();
        isset($config['adminId']) && $role === intval($config['adminId']) && Public_Helper::response(100);
        $node = explode(',',$node);
        //这里菜单等级一定不能出错


        $up = Model::factory('Auth')->updateRoleNode($role, $node);
        !$up ? Public_Helper::response(201):Public_Helper::response(0);
    }

    //获取指定角色可以显示的菜单(默认是当前用户角色)
    public function action_get_role_node()
    {
        $role = $this->request->post('role');
        if(is_null($role)) {
            $user = Session::instance ()->get ( 'user' );
            $role = $user[ 'role' ];
        }
        $role = (int) $role;

        $levelNode = array();
        $roleNodeId = Model::factory ( "Auth" )->roleNode ( $role );
        $roleNodeId = array_column($roleNodeId,'node_id');

        //角色所拥有的节点不为空,除了超级管理员
        $config = Kohana::$config->load('base')->as_array();
        if(!empty($roleNodeId) || (isset($config['adminId']) && $role===intval($config['adminId']))) {
            $this->node = Model::factory ( 'Auth' )->node ( $roleNodeId );
            $levelNode = $this->getChild ();
        }
        Public_Helper::response(0,$levelNode);
    }

    //获取全部节点
    public function action_get_all_node()
    {
        $this->node = Session::instance()->get('node');
        if(is_null($this->node)){
            $this->node = Model::factory ( 'Auth' )->node ();
            Session::instance()->set("node",$this->node);
        }

        $allNode = $this->getChild();
        Public_Helper::response(0,$allNode);
    }


    //新增节点
    public function action_node_add()
    {
        $pid  = $this->request->post('pid');
        $count = Model::factory('Auth')->node(array($pid));
        empty($count) && Public_Helper::response(101);

        $param = array(
            'name'  =>  $this->request->post('name'),
            'path'  =>  $this->request->post('path'),
            'pid'   =>  $pid
        );
        $add = Model::factory('Auth')->nodeAdd($param);
        $add === false ? Public_Helper::response(201):Public_Helper::response(0);
    }












    

    /***********************************************************************************/
    /***********************************************************************************/
    private function getChild($pid=0)
    {
        $childNode = $this->getNodeByPid($pid);
        $result = array();
        foreach($childNode as $value)
        {
            $getChild = $this->getChild($value['id']);
            !empty($getChild) && $value['child'] = $getChild;
            $result[] = $value;
        }
        return $result;
    }
    private function getNodeByPid($pid)
    {
        $result = array();
        foreach($this->node as $value)
        {
            $value['pid'] == $pid && $result[] = $value;
        }
        return $result;
    }


}